package gson.emlog.activity;

import gson.emlog.R;
import gson.emlog.adapter.SortAdapter;
import gson.emlog.utils.Action;
import gson.emlog.utils.Http;
import gson.emlog.utils.Result;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class Sort extends Activity {
	private ListView lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simpitem);

		lv = (ListView) findViewById(R.id.simpList);
		lv.setOnItemLongClickListener(lvLongClick);
		
		Button newBtn = (Button) findViewById(R.id.simp_new);
		newBtn.setOnClickListener(newBtnOnClick);
		
		Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				Http http = new Http(Sort.this);
				Result obj = http.post(Action.BLOG_SORT);
				JSONArray data = new JSONArray();
				if (obj.getStatus()) {
					data = (JSONArray) obj.getData();
				}
				SortAdapter sadpt = new SortAdapter(data, Sort.this);
				lv.setAdapter(sadpt);
			}
		};
		handler.postDelayed(runnable, 500);
	}

	private View.OnClickListener newBtnOnClick = new View.OnClickListener() {
		public void onClick(View v) {
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.sortadd,null);
			Builder alert = new AlertDialog.Builder(Sort.this);
			alert.setTitle("新增"+getResources().getString(R.string.sort));
			alert.setView(layout);
			alert.setPositiveButton("确认", null);
			alert.setNegativeButton("取消", null);
			alert.show();
		}
	};
	
	private AdapterView.OnItemLongClickListener lvLongClick = new AdapterView.OnItemLongClickListener(){
		public boolean onItemLongClick(AdapterView<?> adpt, View view,
				int potions, long id) {
			Builder alert = new AlertDialog.Builder(Sort.this);
			alert.setTitle(R.string.ctrol);
			Resources rs = getResources();
			final String[] its = new String[] {
					rs.getString(R.string.delete),
					rs.getString(R.string.update)};

			alert.setItems(its, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					/*
					 * ad变量用final关键字定义，因为在隐式实现的Runnable接口 的run()方法中
					 * 需要访问final变量。
					 */
					final AlertDialog ad = new AlertDialog.Builder(Sort.this)
					.setMessage(
							"你选择的是：" + which + ": " + its[which])
							.show();

					Handler handler = new Handler();
					Runnable runnable = new Runnable() {
						public void run() {
							// 调用AlertDialog类的dismiss()方法关闭对话框，也可以调用cancel()方法。
							ad.dismiss();

						}
					};
					// 5秒后运行run()方法。
					handler.postDelayed(runnable, 5 * 1000);

				}
			});
			alert.create().show();
			return false;
		}
	};
}
